package com.gelsanalyzer.business;

// Generated Feb 22, 2008 10:40:04 AM by Hibernate Tools 3.2.0.CR1

import static org.hibernate.criterion.Example.create;

import java.util.*;

import org.apache.commons.logging.*;
import org.hibernate.*;

import com.gelsanalyzer.core.*;

/**
 * Home object for domain model class Course.
 * 
 * @see com.gelsanalyzer.business.Course
 * @author Hibernate Tools
 */
public class CourseHome {

    private static final Log log = LogFactory.getLog(CourseHome.class);

    private final SessionFactory sessionFactory = HibernateUtil
        .getSessionFactory();

    // protected SessionFactory getSessionFactory() {
    // try {
    // return (SessionFactory) new InitialContext()
    // .lookup("SessionFactory");
    // } catch ( Exception e ) {
    // log.error("Could not locate SessionFactory in JNDI", e);
    // throw new IllegalStateException(
    // "Could not locate SessionFactory in JNDI");
    // }
    // }

    public void persist(Course transientInstance) {
        log.debug("persisting Course instance");
        try {
            sessionFactory.getCurrentSession().persist(transientInstance);
            log.debug("persist successful");
        } catch ( RuntimeException re ) {
            log.error("persist failed", re);
            throw re;
        }
    }

    public void attachDirty(Course instance) {
        log.debug("attaching dirty Course instance");
        try {
            sessionFactory.getCurrentSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch ( RuntimeException re ) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void attachClean(Course instance) {
        log.debug("attaching clean Course instance");
        try {
            sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch ( RuntimeException re ) {
            log.error("attach failed", re);
            throw re;
        }
    }

    public void delete(Course persistentInstance) {
        log.debug("deleting Course instance");
        try {
            sessionFactory.getCurrentSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch ( RuntimeException re ) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public Course merge(Course detachedInstance) {
        log.debug("merging Course instance");
        try {
            Course result = (Course) sessionFactory.getCurrentSession().merge(
                detachedInstance);
            log.debug("merge successful");
            return result;
        } catch ( RuntimeException re ) {
            log.error("merge failed", re);
            throw re;
        }
    }
    
    public List<Course> findAllCourses() {
        log.debug("finding All Courses");
        
        try {
            org.hibernate.Session session = sessionFactory.getCurrentSession();
            session.beginTransaction();

            List<Course> results = (List<Course>) session
                .createQuery("from Course c").list();
            session.getTransaction().commit();

            log.debug("find All Courses sucessful, result size: "
                    + results.size());
            
            return results;
        } catch ( RuntimeException e ) {
            log.error("find All Courses failed");
            throw e;
        }
    }

    public Course findById(int id) {
        log.debug("getting Course instance with id: " + id);
        try {
            Session session = sessionFactory.getCurrentSession();
            session.beginTransaction();
            
            Course instance = (Course) session.get(
                "com.gelsanalyzer.business.Course", id);
            if ( instance == null ) {
                log.debug("get successful, no instance found");
            } else {
                log.debug("get successful, instance found");
            }
            
            session.getTransaction().commit();
            
            return instance;
        } catch ( RuntimeException re ) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List<Course> findByExample(Course instance) {
        log.debug("finding Course instance by example");
        try {
            List<Course> results = (List<Course>) sessionFactory
                .getCurrentSession().createCriteria(
                    "com.gelsanalyzer.business.Course").add(create(instance))
                .list();
            log.debug("find by example successful, result size: "
                    + results.size());
            return results;
        } catch ( RuntimeException re ) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    public List<Course> findByDepartmentExample(Department department) {
        log.debug("finding Courses by DepartmentId");
        if (department == null ) {
            return null;
        }
        try {
            org.hibernate.Session session = sessionFactory.getCurrentSession();
            session.beginTransaction();

            List<Course> results = (List<Course>) session
                .createQuery("from Course c where c.departmentId = "
                        + department.getDepartmentId()).list();
            session.getTransaction().commit();

            log.debug("find by Department Example sucessful, result size: "
                    + results.size());
            
            return results;
        } catch ( RuntimeException e ) {
            log.error("find by department failed");
            throw e;
        }
    }
}
